package com.ruleengine.context.impl;

import com.ruleengine.context.EContextPropertyScope;
import com.ruleengine.context.EContextPropertyType;
import com.ruleengine.context.IContextProperty;

public class DefaultContextProperty<T> implements IContextProperty<T> {
    private String key;
    private T value;
    private EContextPropertyScope scope;
    private EContextPropertyType type;
    private boolean serializable;
    private boolean init;
    
    
    public DefaultContextProperty(String key, EContextPropertyScope scope,
            EContextPropertyType type, boolean serializable) {
        super();
        this.key = key;
        this.scope = scope;
        this.type = type;
        this.serializable = serializable;
        this.init = false;
    }
    
    /**
     * 是否可序列化 .
     * 可以序列化的属性可以跨容器传递
     * @return the serializable
     */
    public boolean isSerializable() {
        return serializable;
    }

    /**
     * 设置可序列化标志
     * @param serializable the serializable to set
     */
    public void setSerializable(boolean serializable) {
        this.serializable = serializable;
    }

    public DefaultContextProperty(String key, EContextPropertyScope scope) {
        this(key, scope, EContextPropertyType.VARIABLE, false);
    }

    public DefaultContextProperty(String key) {
        this(key, EContextPropertyScope.PUBLIC, EContextPropertyType.VARIABLE, false);
    }

    /* (non-Javadoc)
     * @see com.wx.workflow.engine.context.IContextProperty#Clone()
     */
    public T clone() throws CloneNotSupportedException{
        // TODO Auto-generated method stub
        return (T)super.clone();
    }

    /* (non-Javadoc)
     * @see com.wx.workflow.engine.context.IContextProperty#getKey()
     */
    public String getKey() {
        // TODO Auto-generated method stub
        return key;
    }

    /* (non-Javadoc)
     * @see com.wx.workflow.engine.context.IContextProperty#getScope()
     */
    public EContextPropertyScope getScope() {
        // TODO Auto-generated method stub
        return scope;
    }

    /* (non-Javadoc)
     * @see com.wx.workflow.engine.context.IContextProperty#getValue()
     */
    public T getValue() {
        // TODO Auto-generated method stub
        return value;
    }

    /* (non-Javadoc)
     * @see com.wx.workflow.engine.context.IContextProperty#setValue(java.lang.Object)
     */
    public void setValue(T value) {
        // TODO Auto-generated method stub
        if(type==EContextPropertyType.CONSTANT && this.init){
            throw new RuntimeException("the property is constant and had already been initialized");
        }
        this.value = value;
        this.init = true;
    }

    /* (non-Javadoc)
     * @see com.wx.workflow.engine.context.IContextProperty#getType()
     */
    public EContextPropertyType getType() {
        // TODO Auto-generated method stub
        return type;
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if(this==obj){
            return true;
        }else if( ! (obj instanceof DefaultContextProperty)){
            return false;
        }else{
            DefaultContextProperty p = (DefaultContextProperty)obj;
            if(!p.key.equals(p.getKey())){
                return false;
            }else if( p.type != p.getType()){
                return false;
            }else if( p.scope != p.getScope()){
                return false;
            }
            return true;
        }
    }

}
